<template>
    <el-tree

      ref="tree"
      node-key="id"
      :data="treeData"
      :props="defaultProps"
      :highlight-current="true"
      :default-expand-all="true"
      empty-text=" "
      @node-click="handleNodeClick"
      style="background:none; padding: 10px 5px 30px 10px;">

    </el-tree>
</template>
<script>

export default {

  data () {
    return {
      /* tree */
      treeData: [],

      defaultProps: {
        children: 'children',
        label: 'label'
      },
      map: {},
      routes: [],

      // 定义左侧菜单中没有的页面 url 标题映射关系，用于初始化加载,不要包含新增、修改字样
      staticFormPages: {
        '/page/document/document_form': '文书模板',
        'src/page/document/elment_template_form': '要素模板',
        '/page/case/dispute_detail': '纠纷详情',
        '/page/case/case_detail': '案件详情',
        '/page/user/company_detail': '企业详情',
        '/page/user/worker': '员工信息',
        '/page/user/company_form': '企业信息',

        '/page/user/identity_auth': '知产认证审核',

        '/page/demo/test': 'test'

      },
      // 等待app.vue加载逻辑完成
      hold: true
    }
  },
  getRoutes () {
    return this.routes
  },
  mounted () {

  },
  created () {
    this.loadMenu()
  },
  computed: {
    allTreeNode () {
      return this.map
    }
  },
  props: [
    'selectedTreeNodeId'
  ],
  watch: {
    selectedTreeNodeId: (val) => {}
  },
  methods: {
    releaseHold () {
      this.hold = false
    },
    loadMenu () {
      var _this = this
      // 初始化
      // 当初始加载的url后有参数时（例： http://127.0.0.1:8010/#/pages/about2）， 需要直接打开相应的tab页，并定位树节点

      var currentHash = window.location.hash
      if (currentHash && currentHash.indexOf('page') != -1) {
        var pageUri = currentHash.substring(1)
        var paramObj = {}

        // 加载时url后带参数
        if (pageUri.indexOf('?') != -1) {
          var uriHenad = pageUri.substring(0, pageUri.indexOf('?'))

          var paramAppend = pageUri.substring(pageUri.indexOf('?') + 1)

          var tmpArrays = paramAppend.split('&')

          for (var i = 0; i < tmpArrays.length; i++) {
            var tmps = tmpArrays[i].split('=')
            if (tmps.length == 2) {
              paramObj[tmps[0]] = tmps[1]
            }
          }

          pageUri = uriHenad
        }

        // 先从标签页面找
        var directLinkPage
        if (_this.staticFormPages[pageUri]) {
          _this.$emit('treeNodeClick', {
            label: _this.staticFormPages[pageUri],
            params: paramObj,
            uri: pageUri
          })
        } else {
          directLinkPage = {
            url: pageUri,
            params: paramObj
          }
        }
      }

      _this.$ajax({
        url: '/asama/user/MenuRpc/tree.json',
        method: 'post',
        data: {
          securityId: _this.securityId
        },
        success: function (responseData) {
          if (responseData.data && responseData.data.rows) {
            var tmpArray = responseData.data.rows

            var resultArray = []

            for (var i = 0; i < tmpArray.length; i++) {
              for (var j = 0; j < tmpArray[i].children.length; j++) {
                resultArray.push(tmpArray[i].children[j])
              }
            }

            _this.treeData = resultArray

            _this.fillId(_this.treeData)
            _this.fillMapData(_this.treeData)

            // 初始加载页面

            if (directLinkPage) {
              var treeNode = _this.selectNodeByUri(directLinkPage.url)

              if (treeNode) {
                treeNode.params = directLinkPage.params

                // _this.$refs.tree.setCurrentNode(treeNode);

                _this.$emit('treeNodeClick', treeNode)

                window.setTimeout(function () {
                  _this.selectTreeId(treeNode.id)
                }, 200)

                treeNode.params = {}
              }
            }
          }
        }
      })
    },

    handleNodeClick (treeNode) {
      if (treeNode.loadMore) {

      } else {
        this.$emit('treeNodeClick', treeNode)
      }
    },

    selectTreeId (treeNodeId) {
      this.$refs.tree.setCurrentKey(treeNodeId)
    },

    selectNodeByUri (uri) {
      return this.allTreeNode[uri]
    },

    fillMapData: function (parnetData) {
      if (!parnetData) {
        return
      }
      if (Array.isArray(parnetData)) {
        for (var i = 0; i < parnetData.length; i++) {
          this.fillMapData(parnetData[i])
        }
      } else {
        if (parnetData.uri) {
          this.map[parnetData.uri] = parnetData
          this.routes.push({
            'path': parnetData.uri,
            'name': parnetData.uri.split('/').join('_')
          })
        } else {
          if (parnetData.children && parnetData.children.length > 0) {
            this.fillMapData(parnetData.children)
          }
        }
      }
    },
    fillId: function (parnetData) {
      if (!parnetData) {
        return
      }

      if (Array.isArray(parnetData)) {
        for (var i = 0; i < parnetData.length; i++) {
          this.fillId(parnetData[i])
        }
      } else {
        parnetData.id = 'tree' + parseInt(Math.random() * 100000)

        if (parnetData.children && parnetData.children.length > 0) {
          this.fillId(parnetData.children)
        }
      }
    }

  }
}
</script>
<style>
</style>
